<template>
    <div class="human-ai">
        <el-button @click="createHuman">人员创建</el-button>
        <el-button @click="destroyHuman">人员销毁</el-button>
        <el-button @click="startTracking">开启跟踪模式</el-button>
        <el-button @click="destroyTracking">取消跟踪模式</el-button>
    </div>
</template>

<script setup lang="ts">
import { onMounted, ref } from "vue";
import { getMyProjectManager } from "@/getMyProjectManager";
import { viewType } from "@/sctipts/dataTypes";
import { PickedInfo } from "xbsj-xe2/dist-node/xe2-base-objects"
import { ElMessage } from "element-plus";
import { ESHumanPoiActor } from "@/sctipts/thingManager/ESHumanPoiActor";
const myProjectManager = getMyProjectManager()
let human: ESHumanPoiActor | undefined
// @ts-ignore
let timer

// 创建人员
const createHuman = () => {
    if (human) {
        ElMessage.warning('随机人员已经存在')
        return
    }
    human = myProjectManager.createSceneObject(ESHumanPoiActor)
    if (!human) return
    human.position = [114.0426606231189, 38.067354306556624, 286.02736510783143]
    human.name = "Ai"
    human.mode = "suitMan" //["陌生人", "stranger"], ["男士", 'suitMan'], ["女士", 'suitWoman']
    human.animation = "walking"
    human.poiMode = 'ManNormal' //['Stranger', 'Stranger'], ['ManNormal', 'ManNormal'],['ManAbnormal', 'ManAbnormal'],['WomanNormal', 'WomanNormal'],['WomanAbnormal', 'WomanAbnormal'],
    // 设置头像
    setTimeout(() => {
        human.human.poi.style = { "customPicture": "https://preview.qiantucdn.com/58pic/71/00/18/53n58PICPnzmqGtYCyb5N_origin_PIC2018.jpg!w1024_new_small_1", "picture": true }
    }, 2000)
    timer = setInterval(() => {
        const position = getRandomPoint()
        human && human.aiMoveTo(position as [number, number, number], 20)
    }, 20000)
    console.log("人员", human);
    human.allowPicking = true
    // dispose意义就是人员销毁，拾取事件也销毁
    human.dispose(human.pickedEvent.don((e) => {
        const result = PickedInfo.getFinalAttachedInfo(e)
        if (result && result == 'globelPick') {
            human && (human.highlight = true);
            ElMessage.success('我被拾取了')
        }
    }))


}

// 销毁人员
const destroyHuman = () => {
    if (human) {
        //@ts-ignore
        clearInterval(timer)
        myProjectManager.destroySceneObject(human)
        human = undefined
    }
}

// 跟踪模式
const startTracking = () => {
    if (human && myProjectManager.activeViewer) {
        // 跟随模式
        // @ts-ignore
        myProjectManager.activeViewer.changeToFollow(human._human.human.id, 10, 20, -60)
    } else {
        ElMessage.warning('人员不存在，无法跟踪')
    }
}

// 取消跟踪模式
const destroyTracking = () => {
    if (myProjectManager.activeViewer) {
        myProjectManager.activeViewer.changeToMap()
    }

}

/**
 * 随机生成点位
 */
const getRandomPoint = () => {
    // 定义矩形的边界
    const x1 = 114.0430282245739;
    const y1 = 38.06721444007653;
    const z1 = 285.0274330260983;

    const x2 = 114.04226659263986;
    const y2 = 38.0674362537794;
    const z2 = 285.02749800431064;

    const x3 = 114.04304295433533;
    const y3 = 38.067375792960995;
    const z3 = 285.0274464448833;

    const x4 = 114.04224977372188
    const y4 = 38.06727234350286
    const z4 = 285.02745973642135

    // 计算边界的最大值和最小值
    const minX = Math.min(x1, x2, x3, x4);
    const maxX = Math.max(x1, x2, x3, x4);
    const minY = Math.min(y1, y2, y3, y4);
    const maxY = Math.max(y1, y2, y3, y4);
    const minZ = Math.min(z1, z2, z3, z4);
    const maxZ = Math.max(z1, z2, z3, z4);

    // 生成随机点
    const randomX = minX + (maxX - minX) * Math.random();
    const randomY = minY + (maxY - minY) * Math.random();
    const randomZ = minZ + (maxZ - minZ) * Math.random();

    return [randomX, randomY, randomZ]
}



</script>
<style scoped></style>